package demo
{
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.Joints.b2JointDef;
	import Box2D.Dynamics.Joints.b2WeldJoint;
	import Box2D.Dynamics.Joints.b2WeldJointDef;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	
	import flash.display.Sprite;
	import flash.events.Event;

	public class Scene
	{
		public const m_physScale:Number = 1 / 30;
		public var m_world:b2World;
		protected var s:Sprite;
		public function Scene(s:Sprite)
		{
			this.s = s;
			// Define the gravity vector
			var gravity:b2Vec2 = new b2Vec2(0.0, 0.0);
			// Construct a world object
			m_world = new b2World(gravity, true);
			//m_world.SetBroadPhase(new b2BroadPhase(worldAABB));
			m_world.SetWarmStarting(true);
			// set debug draw
			var dbgDraw:b2DebugDraw = new b2DebugDraw();
			//var dbgSprite:Sprite = new Sprite();
			//m_sprite.addChild(dbgSprite);
			//设置临时的颜色填充
			dbgDraw.SetSprite(s);
			dbgDraw.SetDrawScale(30.0);
			dbgDraw.SetFillAlpha(0.3);
			dbgDraw.SetLineThickness(1.0);
			dbgDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
			m_world.SetDebugDraw(dbgDraw);
			
			// Create border of boxes
			var wall:b2PolygonShape = new b2PolygonShape();
			var wallBd:b2BodyDef = new b2BodyDef();
			wallBd.type = b2Body.b2_staticBody;
			var wallB:b2Body;
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = wall;
			fixtureDef.density = 1000.0;//密度
			fixtureDef.friction = 0.3;//摩擦力
			fixtureDef.restitution = 0.6;//弹力
			fixtureDef.isSensor = false;
			
			//900 * 500
			//左和右
			wall.SetAsBox(getSize(100), getSize(600));//创建一个尺寸
			// Left
			wallBd.position.Set(getPosi(0), getPosi(300));//设置墙体位置
			wallB = m_world.CreateBody(wallBd);//在物理世界中建立墙体
			wallB.CreateFixture(fixtureDef);
//			wallB.CreateFixture2(wall);//讲墙体尺寸设置到body对象
			// Right
			wallBd.position.Set(getPosi(1000), getPosi(300));
			wallB = m_world.CreateBody(wallBd);
			wallB.CreateFixture(fixtureDef);
//			wallB.CreateFixture2(wall);
			
			//上和下
			wall.SetAsBox(getSize(1000), getSize(100));
			// Top
			wallBd.position.Set(getPosi(500), getPosi(0));
			wallB = m_world.CreateBody(wallBd);
			wallB.CreateFixture(fixtureDef);
//			wallB.CreateFixture2(wall);
			// Bottom
			wallBd.position.Set(getPosi(500), getPosi(600));
			wallB = m_world.CreateBody(wallBd);
			wallB.CreateFixture(fixtureDef);
//			wallB.CreateFixture2(wall);
			
			//桌子
			var face:b2Body = this.DrawBox(250, 490, 200, 10, true, 0, 1, 50, 0.1);
			
			var tui1:b2Body = this.DrawBox(133, 518, 100, 10, true, ((-20 * Math.PI) / 180), 1, 50, 0.1);
			var _local4:b2WeldJointDef = new b2WeldJointDef();
			_local4.Initialize(face, tui1, new b2Vec2((250 * m_physScale), (490 * m_physScale)));
			(m_world.CreateJoint((_local4 as b2JointDef)) as b2WeldJoint);
			
			var tui2:b2Body = this.DrawBox(368, 518, 100, 10, true, ((20 * Math.PI) / 180), 1, 50, 0.1);
			_local4 = new b2WeldJointDef();
			_local4.Initialize(face, tui2, new b2Vec2((250 * m_physScale), (490 * m_physScale)));
			(this.m_world.CreateJoint((_local4 as b2JointDef)) as b2WeldJoint);
			
			s.stage.addEventListener(Event.ENTER_FRAME, run);
		}
		
		public function DrawBox(_arg1:Number, _arg2:Number, _arg3:Number, _arg4:Number, _arg5:Boolean, _arg6:Number, _arg7:Number, _arg8:Number, _arg9:Number):b2Body{
			var _local10:b2BodyDef = new b2BodyDef();
			_local10.position.Set((_arg1 * m_physScale), (_arg2 * m_physScale));
			_local10.angle = _arg6;
			if (_arg5){
				_local10.type = b2Body.b2_dynamicBody;
			};
			var _local11:b2PolygonShape = new b2PolygonShape();
			_local11.SetAsBox(((_arg3 / 2) * m_physScale), ((_arg4 / 2) * m_physScale));
			var _local12:b2FixtureDef = new b2FixtureDef();
			_local12.density = _arg8;
			_local12.friction = _arg7;
			_local12.restitution = _arg9;
			_local12.shape = _local11;
			var _local13:b2Body = this.m_world.CreateBody(_local10);
			_local13.CreateFixture(_local12);
			return (_local13);
		}
		
		protected function run(e:Event):void{
			m_world.Step(m_physScale, 10, 10);
			m_world.ClearForces();
			m_world.DrawDebugData();
		}
		
		public function getSize(value:Number):Number{
			return value * m_physScale / 2;
		}
		public function getPosi(value:Number):Number{
			return value * m_physScale;
		}
	}
}